//
//  LoginViewController.m
//  AppleMarket
//
//  Created by yangy on 22/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "LoginViewController.h"

// view
#import "LoginMainView.h"
// videModel
#import "LoginViewModel.h"
// controller
#import "InputCodeViewController.h"
#import "RegistViewController.h"
#import "ForgetPwdViewController.h"
#import "RDVTabBarController.h"
#import "SettingViewController.h"
// model
#import "ImageCodeMoel.h"

@interface LoginViewController ()
@property (nonatomic, strong) LoginMainView *mainView;
@property (nonatomic, strong) LoginViewModel *viewModel;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainView];
    [self setNagivationBar];
    [self bindViewModel];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    RDVTabBarController *tabbarVC = (RDVTabBarController*)kAppDelegate.window.rootViewController;
    UINavigationController *navRoot = [tabbarVC.viewControllers objectAtIndex:tabbarVC.selectedIndex];
    if(navRoot.viewControllers.count > 1){
        [tabbarVC setTabBarHidden:YES animated:YES];
    }
}

- (void) bindViewModel
{
    @weakify(self);
    [self.viewModel.needInputCodeSubject subscribeNext:^(id  _Nullable x) {
        // 登录的时候检查到需要输入验证码登录
        @strongify(self);
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeLogin];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel * _Nonnull codeModel,NSString *inputCodeStr) {
            [self.viewModel.loginCommond execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.loginSuccesSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        // 登录成功
        [self dismissViewControllerAnimated:YES completion:nil];
        // 刷新用户资产
    }];
    
    [self.viewModel.forgetPwdClickSubject subscribeNext:^(id  _Nullable x) {
       // 忘记密码
        ForgetPwdViewController *forgetVC = [[ForgetPwdViewController alloc] init];
        [self bh_PushViewController:forgetVC hiddenTabbar:YES];
    }];
    
}

- (void) setNagivationBar
{
    @weakify(self);
    UIButton *leftButton = [UIButton cm_ButtonWithTitle:@"取消" font:kFont(13) titleColor:kColorWithHex(0xADB7C1)];
    [[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"注册" font:kFont(13) titleColor:kColorWithHex(0x3291EB)];
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        RegistViewController *registVC = [[RegistViewController alloc] init];
        [self bh_PushViewController:registVC hiddenTabbar:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (LoginMainView *)mainView
{
    if(!_mainView){
        _mainView = [[LoginMainView alloc] initWithFrame:self.view.bounds viewModel:self.viewModel];
    }
    return _mainView;
}

- (LoginViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[LoginViewModel alloc] init];
    }
    return _viewModel;
}

@end
